/* 
*
* 1、园区楼栋能耗分布
* 2、动力中心能耗分布图
*/
(function () {
    // 园区楼栋能耗分布
    var legendData = [];
    var seriesData = [];
    var colors = [];
    // 动力中心 横纵坐标
    var xValues = [];
    var yValues = [];
    $.ajax({
        url: window.base_url + '/ccp/electryShow/showByDays',
        type: 'post',
        dataType: 'json',
        data: {
            token: window.token,
            time: getPreDay(),
            parkId: 9,
            days: 1,
        },
        success: function (res) {
            // console.log(res)
            if (res.retcode == 200) {
                if (res.data.dongliPart.partList != null) {
                    res.data.dongliPart.partList.forEach(function(item, index){
                        xValues.push(item.title);
                        yValues.push({
                            value: Math.abs(item.value),
                            name: item.title
                        });
                    })
                    $('#dlpue').html('PUE&nbsp;'+res.data.dongliPart.xishu);
                }
                if (res.data.dayPart.partList && res.data.dayPart.partList.length) {
                    const dayData = res.data.dayPart.partList
                    dayData.forEach(function(item) {
                        if(item.title === '客服楼') {
                            item.title = 'CC-1'
                        }
                        legendData.push({
                            name: item.title,
                            icon: 'circle'
                        });
                        seriesData.push({
                            value: item.value, 
                            name: item.title
                        });
                        colors.push(item.color)
                    })
                }
            }
            // 动力能耗分布图
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b} : {c} ({d}%)",
                    color: 'rgba(255,255,255,0)'
                },
                legend: {
                    left: 'left',
                    data: xValues,
                    textStyle: {
                        color: 'rgba(255,255,255,0.8)'
                    }
                },
                color: ['rgba(43,153,255, 0.8)', 'rgba(35,249,236, 0.8)', 'rgba(141,200,255, 0.8)'],
                series: [
                    {
                        name: '动力中心',
                        type: 'pie',
                        radius: '35%',
                        center: ['50%', '60%'],
                        label: {
                            normal: {
                                position: 'outside',
                                formatter: '{b|{b}：} \n {c} \n {per|{d}%}',
                                backgroundColor: 'rgb(33,61,95)',
                                align: 'center',
                                borderColor: 'rgb(33,61,95)',
                                color: '#fff',
                                borderWidth: 1,
                                borderRadius: 4,
                                lineHeight: 16,
                                fontSize: 10,
                                padding: [4,4],
                                rich: {
                                    a: {
                                        color: '#999',
                                        lineHeight: 22,
                                        align: 'center'
                                    },
                                    hr: {
                                        borderColor: '#aaa',
                                        width: '100%',
                                        borderWidth: 0.5,
                                        height: 0
                                    },
                                    b: {
                                        color: '#fff',
                                        fontSize: 10,
                                    },
                                    c: {
                                        padding: [4, 0],
                                        lineHeight: 15
                                    },
                                    per: {
                                        color: '#fff',
                                    }
                                }
                            }
                        },
                        data: yValues,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            echarts.init(document.getElementById('energyCenter')).setOption(option);
            // 当日能耗分布
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b}: {c} ({d}%)"
                },
                legend: {
                    x: 'top',
                    orient: 'vertical',
                    data: legendData,
                    textStyle: {
                        // fontSize:'0.5vw',
                        color: 'rgba(255,255,255,0.8)'
                    }
                },
                color: colors,
                series: [
                    {
                        name: '昨日能耗分布',
                        type: 'pie',
                        radius: ['30%', '50%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: true,
                                textStyle: {
                                    fontSize: '14',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: seriesData
                    }
                ]
            };
            echarts.init(document.getElementById('energyConsumptionTotal')).setOption(option);  
        },
        error: function (err) {
            console.log(err);
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b} : {c} ({d}%)",
                    color: 'rgba(255,255,255,0)'
                },
                legend: {
                    left: 'left',
                    data: xValues,
                    textStyle: {
                        color: 'rgba(255,255,255,0.8)'
                    }
                },
                color: ['rgba(43,153,255, 0.8)', 'rgba(35,249,236, 0.8)', 'rgba(141,200,255, 0.8)'],
                series: [
                    {
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        labelLine: {
                            show: false
                        },
                        label: {
                            show: false
                        },
                        data: yValues,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            echarts.init(document.getElementById('energyCenter')).setOption(option);
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b}: {c} ({d}%)"
                },
                legend: {
                    x: 'top',
                    orient: 'vertical',
                    data: legendData,
                    textStyle: {
                        // fontSize:'0.5vw',
                        color: 'rgba(255,255,255,0.8)'
                    }
                },
                color: colors,
                series: [
                    {
                        name: '当日能耗分布',
                        type: 'pie',
                        radius: ['30%', '50%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: true,
                                textStyle: {
                                    fontSize: '14',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: seriesData
                    }
                ]
            };
            echarts.init(document.getElementById('energyConsumptionTotal')).setOption(option);  
        }
    })
     
    
    function getPreDay() {  // 获取前一天时间
        let myTime = new Date();
        let year = myTime.getFullYear();
        let month = myTime.getMonth() + 1;
        let day = myTime.getDate();
        let arr = [];
        day = day - 1  // 取昨天日期
        if (day == 0) {
            if ((month - 1) == 1 || (month - 1) == 3 || (month - 1) == 5 || (month - 1) == 7 || (month - 1) == 8 || (month - 1) == 10 || (month - 1) == 12 || (month - 1) == 0) {
                day = 31;
            } else if ((month - 1) == 2) {
                // 闰年 1种是 能被4整除且不能被100整除 2种 能被400整除
                if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                    day = 28;
                } else {
                    day = 29;
                }
            } else {
                day = 30;
            }
            month = (month - 1);
            if (month == 0) {
                month = 12;
                year = year - 1;
            }
            // console.log(month)
        }
        arr.push(year);
        arr.push(addZero(month));
        arr.push(addZero(day));
        return arr.join('-');
    }
    function addZero(n) {
        if (n < 10) {
            n = '0' + n
        }
        return n
    }
})()